package com.future.hedongjun.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.adapter.CouponAdapter;
import com.future.hedongjun.mall.bean.CouponBean;
import com.future.hedongjun.mine.bean.response.CouponListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CouponListActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int type = 1;
    private double amount = -1;
    private CouponAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        ButterKnife.bind(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CouponAdapter(null);
        recyclerView.setAdapter(adapter);
        View emptyView = getLayoutInflater().inflate(R.layout.layout_empty_view_default, null);
        if (type == 1) {
            setTitle("我的优惠券");
            ((TextView) emptyView.findViewById(R.id.tv_empty)).setText("暂无优惠券");
        } else {
            setTitle("优惠券");
            adapter.setOnItemClickListener(this);
            ((TextView) emptyView.findViewById(R.id.tv_empty)).setText("暂无可用优惠券");
        }
        adapter.setEmptyView(emptyView);
        getData();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        type = extras.getInt("type");
        amount = extras.getDouble("amount");
    }


    private void getData() {
        new HttpRequest<CouponListResponse>()
                .with(this)
                .setApiCode("_canusediscount_001")
                .addParam("userToken", MyApp.getInstance().getUserToken())
                .addParam("amount", amount == -1 ? "" : "" + amount)
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        adapter.setNewData( ((CouponListResponse) response).data);
                    }
                }).start(new CouponListResponse());
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent data = new Intent();
        data.putExtra("coupon", ((CouponBean) adapter.getItem(position)));
        setResult(0x123, data);
    }
}
